package com.medical.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.model.entity.Knowledge;
import com.medical.service.KnowledgeService;
import com.medical.domain.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {

    @Resource
    private KnowledgeService knowledgeService;

    // 新增知识库条目
    @PostMapping
    public R<Boolean> addKnowledge(@RequestBody Knowledge knowledge) {
        return R.ok(knowledgeService.save(knowledge));
    }

    // 删除知识库条目
    @DeleteMapping("/{id}")
    public R<Boolean> deleteKnowledge(@PathVariable Long id) {
        return R.ok(knowledgeService.removeById(id));
    }

    // 更新知识库条目
    @PutMapping
    public R<Boolean> updateKnowledge(@RequestBody Knowledge knowledge) {
        return R.ok(knowledgeService.updateById(knowledge));
    }

    // 查询单个条目
    @GetMapping("/{id}")
    public R<Knowledge> getKnowledge(@PathVariable Long id) {
        return R.ok(knowledgeService.getById(id));
    }

    // 分页查询所有条目
    @GetMapping("/list")
    public R<Page<Knowledge>> listKnowledges(@RequestParam Integer pageNum,
                                             @RequestParam Integer pageSize) {
        Page<Knowledge> page = knowledgeService.page(new Page<>(pageNum, pageSize));
        return R.ok(page);
    }
}